<template>
  <div class="addPatient">
    <div class="iptRow">
      <el-input v-model="patientName" placeholder="输出患者姓名"></el-input>
      <el-input v-model="phoneNumber" placeholder="输出手机号码"></el-input>
      <el-input v-model="idNumber" placeholder="输出身份证"></el-input>
      <el-select v-model="sex" placeholder="性别">
        <el-option
          v-for="item in sexOptions"
          :key="item.value"
          :label="item.label"
          :value="item.value"
        ></el-option>
      </el-select>
      <el-button icon="el-icon-search" circle @click="getPatientList()"></el-button>
    </div>
    <div class="btnRow">
      <el-button type="primary" @click="addPatient">新增用户</el-button>
    </div>
    <div class="record">
      <el-table :data="tableData" style="width: 100%">
        <el-table-column label="操作" width="200">
          <template slot-scope="scope">
            <el-link type="success" @click="medicine(scope.row)">开药</el-link>
            <el-link type="primary" @click="app(scope.row)">医生挂号</el-link>
          </template>
        </el-table-column>
        <!-- <el-table-column prop="patientId" label="用户id"></el-table-column> -->
        <el-table-column prop label="序号">
          <template slot-scope="scope">
            <span>{{ scope.$index + 1 + (pageIndex - 1) * pageSize }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="sex" label="性别">
          <template slot-scope="scope">
            <span>{{ scope.row.sex == "1" ? "男" : "女" }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="patientName" label="用户姓名"></el-table-column>
        <el-table-column prop="phoneNumber" label="手机号码"></el-table-column>
        <el-table-column prop="idNumber" label="身份证号码"></el-table-column>
        <el-table-column prop="birth" label="出生日期"></el-table-column>
      </el-table>
    </div>
    <div class="page">
      <el-pagination
        align="right"
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="pageIndex"
        :page-sizes="[5, 10]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next"
        :total="totalNumber"
        style="margin-top: 58px; margin-bottom: 40px"
      ></el-pagination>
    </div>
    <el-dialog title="新增用户" :visible.sync="dialogVisible" width="30%" :before-close="handleClose">
      <el-form ref="form" :model="form" label-width="80px">
        <el-form-item label="姓名">
          <el-input v-model="form.patientName"></el-input>
        </el-form-item>
        <el-form-item label="手机号码">
          <el-input v-model="form.phoneNumber"></el-input>
        </el-form-item>
        <el-form-item label="身份证">
          <el-input v-model="form.idNumber"></el-input>
        </el-form-item>
        <el-form-item label="出生日期">
          <el-date-picker v-model="form.birth" type="date" placeholder="选择日期"></el-date-picker>
        </el-form-item>
        <el-form-item label="性别">
          <el-radio v-model="form.sex" label="1">男</el-radio>
          <el-radio v-model="form.sex" label="0">女</el-radio>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="formConfirm">确 定</el-button>
      </span>
    </el-dialog>
    <el-dialog title="用户开药" :visible.sync="dialogVisible2" width="30%" :before-close="handleClose2">
      <el-form ref="form2" :model="form" label-width="80px">
        <el-form-item label="药品">
          <el-cascader
            :options="options"
            :props="props"
            clearable
            @change="casChange"
            v-model="medicines"
          ></el-cascader>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible2 = false">取 消</el-button>
        <el-button type="primary" @click="form2Confirm">确 定</el-button>
      </span>
    </el-dialog>
    <el-dialog title="医生挂号" :visible.sync="dialogVisible3" width="30%" :before-close="handleClose3">
      <el-form ref="form3" label-width="80px">
        <el-form-item label="选择医生">
          <el-select v-model="doc" placeholder="请选择">
            <el-option
              v-for="item in docList"
              :key="item.adminId"
              :label="item.adminName"
              :value="item.adminName"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="备注">
          <el-input v-model="desc" placeholder="请输入内容"></el-input>
        </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible3 = false">取 消</el-button>
        <el-button type="primary" @click="form3Confirm">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: "addPatient",
  props: {
    adminName: {
      type: String,
      default: ""
    }
  },
  data() {
    return {
      patientName: "",
      phoneNumber: "",
      idNumber: "",
      sex: "",
      id: "",
      name: "",
      idNumber: "",
      patientId: "",
      sexOptions: [
        {
          label: "全部",
          value: ""
        },
        {
          label: "男",
          value: "1"
        },
        {
          label: "女",
          value: "0"
        }
      ],
      form: {
        patientName: "",
        phoneNumber: "",
        idNumber: "",
        birth: "",
        sex: ""
      },
      docList: [],
      form2: {},
      tableData: [],
      props: { multiple: true },
      options: [
        {
          value: "抗生素",
          label: "抗生素",
          children: [
            {
              value: "青霉素类",
              label: "青霉素类",
              children: [
                { value: "青霉素", label: "青霉素" },
                { value: "阿莫西林钠氟氯西林", label: "阿莫西林钠氟氯西林" },
                { value: "青霉素钾", label: "青霉素钾" },
                { value: "阿莫西林舒巴坦钠", label: "阿莫西林舒巴坦钠" },
                { value: "普鲁卡因青霉素", label: "普鲁卡因青霉素" },
                { value: "阿莫西林克拉维酸钾", label: "阿莫西林克拉维酸钾" },
                { value: "苄星青霉素", label: "苄星青霉素" },
                { value: "氨苄西林钠舒巴坦", label: "氨苄西林钠舒巴坦" },
                { value: "氯唑西林", label: "氯唑西林" },
                {
                  value: "替卡西林钠克拉维酸钾",
                  label: "替卡西林钠克拉维酸钾"
                },
                { value: "氨苄西林氯唑西林", label: "氨苄西林氯唑西林" },
                { value: "氨苄西林", label: "氨苄西林" },
                { value: "苯唑西林", label: "苯唑西林" },
                { value: "阿洛西林", label: "阿洛西林" },
                { value: "哌拉西林", label: "哌拉西林" },
                { value: "美洛西林", label: "美洛西林" }
              ]
            },
            {
              value: "头孢菌素类",
              label: "头孢菌素类",
              children: [
                { value: "头孢氨苄", label: "头孢氨苄" },
                { value: "头孢他啶", label: "头孢他啶" },
                { value: "头孢唑林", label: "头孢唑林" },
                { value: "头孢曲松", label: "头孢曲松" },
                { value: "头孢拉定", label: "头孢拉定" },
                { value: "头孢哌酮", label: "头孢哌酮" },
                { value: "头孢羟氨苄", label: "头孢羟氨苄" },
                { value: "头孢哌酮舒巴坦", label: "头孢哌酮舒巴坦" },
                { value: "头孢硫脒", label: "头孢硫脒" },
                { value: "拉氧头孢", label: "拉氧头孢" },
                { value: "头孢呋辛", label: "头孢呋辛" },
                { value: "头孢米诺", label: "头孢米诺" },
                { value: "头孢克洛", label: "头孢克洛" },
                { value: "头孢唑肟", label: "头孢唑肟" },
                { value: "头孢美唑", label: "头孢美唑" },
                { value: "头孢妥仑匹酯", label: "头孢妥仑匹酯" },
                { value: "头孢替安", label: "头孢替安" },
                { value: "头孢地秦", label: "头孢地秦" },
                { value: "头孢西丁", label: "头孢西丁" },
                { value: "头孢泊肟酯", label: "头孢泊肟酯" }
              ]
            },
            {
              value: "碳青霉烯类",
              label: "碳青霉烯类",
              children: [
                { value: "厄他培南", label: "厄他培南" },
                { value: "帕尼培南", label: "帕尼培南" },
                { value: "亚胺培南", label: "亚胺培南" },
                { value: "美罗培南", label: "美罗培南" }
              ]
            },
            {
              value: "单环β-内酰胺",
              label: "单环β-内酰胺",
              children: [{ value: "氨曲南", label: "氨曲南" }]
            },
            {
              value: "氨基糖苷类",
              label: "氨基糖苷类",
              children: [
                { value: "卡那霉素", label: "卡那霉素" },
                { value: "依替米星", label: "依替米星" },
                { value: "庆大霉素", label: "庆大霉素" },
                { value: "异帕米星", label: "异帕米星" },
                { value: "妥布霉素", label: "妥布霉素" },
                { value: "西索米星", label: "西索米星" },
                { value: "大观霉素", label: "大观霉素" },
                { value: "小诺米星", label: "小诺米星" },
                { value: "庆大霉素", label: "奈替米星" }
              ]
            },
            {
              value: "酰胺醇类",
              label: "酰胺醇类",
              children: [
                { value: "氯霉素", label: "氯霉素" },
                { value: "甲砜霉素", label: "甲砜霉素" }
              ]
            }
          ]
        },
        {
          value: "合成抗菌药",
          label: "合成抗菌药",
          children: [
            {
              value: "磺胺类",
              label: "磺胺类",
              children: [
                { value: "磺胺嘧啶", label: "磺胺嘧啶" },
                { value: "柳氮磺吡啶", label: "柳氮磺吡啶" },
                { value: "磺胺甲噁唑", label: "磺胺甲噁唑" },
                { value: "磺胺嘧啶银", label: "磺胺嘧啶银" },
                { value: "磺胺甲噁唑-甲氧苄啶", label: "磺胺甲噁唑-甲氧苄啶" },
                { value: "磺胺嘧啶锌", label: "磺胺嘧啶锌" }
              ]
            },
            {
              value: "喹诺酮类",
              label: "喹诺酮类",
              children: [
                { value: "环丙沙星", label: "环丙沙星" },
                { value: "加替沙星", label: "加替沙星" },
                { value: "诺氟沙星", label: "诺氟沙星" },
                { value: "莫西沙星", label: "莫西沙星" },
                { value: "氧氟沙星", label: "氧氟沙星" },
                { value: "依诺沙星", label: "依诺沙星" },
                { value: "氟罗沙星", label: "氟罗沙星" },
                { value: "司帕沙星", label: "司帕沙星" },
                { value: "洛美沙星", label: "洛美沙星" },
                { value: "帕珠沙星", label: "帕珠沙星" },
                { value: "培氟沙星", label: "培氟沙星" },
                { value: "吉米沙星", label: "吉米沙星" }
              ]
            },
            {
              value: "硝基呋喃类",
              label: "硝基呋喃类",
              children: [
                { value: "呋喃妥因", label: "呋喃妥因" },
                { value: "呋喃唑酮", label: "呋喃唑酮" }
              ]
            },
            {
              value: "硝基咪唑类",
              label: "硝基咪唑类",
              children: [
                { value: "甲硝唑", label: "甲硝唑" },
                { value: "奥硝唑", label: "奥硝唑" },
                { value: "替硝唑", label: "替硝唑" }
              ]
            }
          ]
        },
        {
          value: "抗分枝杆菌药",
          label: "抗分枝杆菌药",
          children: [
            {
              value: "抗结核病类",
              label: "抗结核病类",
              children: [
                { value: "吡嗪酰胺", label: "吡嗪酰胺" },
                { value: "乙胺丁醇", label: "乙胺丁醇" },
                { value: "对氨基水杨酸钠", label: "对氨基水杨酸钠" },
                { value: "异烟肼", label: "异烟肼" },
                { value: "利福平", label: "利福平" },
                { value: "丙硫异烟胺", label: "丙硫异烟胺" },
                { value: "链霉素", label: "链霉素" },
                { value: "利福定", label: "利福定" }
              ]
            },
            {
              value: "抗麻风病类",
              label: "抗麻风病类",
              children: [
                { value: "氨苯砜", label: "氨苯砜" },
                { value: "沙利度胺", label: "沙利度胺" },
                { value: "氯法齐明", label: "氯法齐明" }
              ]
            }
          ]
        },
        {
          value: "抗真菌药",
          label: "抗真菌药",
          children: [
            {
              value: "唑类",
              label: "唑类",
              children: [
                { value: "克霉唑", label: "克霉唑" },
                { value: "氟康唑", label: "氟康唑" },
                { value: "咪康唑", label: "咪康唑" },
                { value: "伊曲康唑", label: "伊曲康唑" },
                { value: "酮康唑", label: "酮康唑" },
                { value: "伏立康唑", label: "伏立康唑" }
              ]
            },
            {
              value: "多烯类",
              label: "多烯类",
              children: [
                { value: "制霉素", label: "制霉素" },
                { value: "两性霉素", label: "两性霉素" }
              ]
            },
            {
              value: "其他",
              label: "其他",
              children: [
                { value: "氟胞嘧啶", label: "氟胞嘧啶" },
                { value: "阿莫罗芬", label: "阿莫罗芬" },
                { value: "联苯苄唑", label: "联苯苄唑" },
                { value: "卡泊芬净", label: "卡泊芬净" },
                { value: "环吡酮胺", label: "环吡酮胺" }
              ]
            }
          ]
        },
        {
          value: "抗病毒药",
          label: "抗病毒药",
          children: [
            {
              value: "广谱类",
              label: "广谱类",
              children: [
                { value: "阿昔洛韦", label: "阿昔洛韦" },
                { value: "伐昔洛韦", label: "伐昔洛韦" },
                { value: "利巴韦林", label: "利巴韦林" },
                { value: "喷昔洛韦", label: "喷昔洛韦" },
                { value: "泛昔洛韦", label: "泛昔洛韦" },
                { value: "膦甲酸钠", label: "膦甲酸钠" },
                { value: "更昔洛韦", label: "更昔洛韦" }
              ]
            },
            {
              value: "核苷类逆转录酶抑制剂",
              label: "核苷类逆转录酶抑制剂",
              children: [
                { value: "拉米夫定", label: "拉米夫定" },
                { value: "阿巴卡韦", label: "阿巴卡韦" },
                { value: "齐多夫定", label: "齐多夫定" },
                { value: "恩替卡韦", label: "恩替卡韦" },
                { value: "去羟肌苷", label: "去羟肌苷" },
                { value: "奥司他韦", label: "奥司他韦" },
                { value: "司他夫定", label: "司他夫定" }
              ]
            },
            {
              value: "非核苷类逆转录酶抑制剂",
              label: "非核苷类逆转录酶抑制剂",
              children: [
                { value: "奈韦拉平", label: "奈韦拉平" },
                { value: "利托那韦", label: "利托那韦" },
                { value: "依非韦伦", label: "依非韦伦" },
                { value: "奈非那韦", label: "奈非那韦" }
              ]
            },
            {
              value: "其它",
              label: "其它",
              children: [{ value: "金刚烷胺", label: "金刚烷胺" }]
            }
          ]
        },
        {
          value: "抗寄生虫病药",
          label: "抗寄生虫病药",
          children: [
            {
              value: "抗吸虫病药",
              label: "抗吸虫病药",
              children: [{ value: "吡喹酮", label: "吡喹酮" }]
            },
            {
              value: "抗疟药",
              label: "抗疟药",
              children: [
                { value: "伯氨喹", label: "伯氨喹" },
                { value: "乙胺嘧啶", label: "乙胺嘧啶" },
                { value: "奎宁", label: "奎宁" },
                { value: "哌喹", label: "哌喹" },
                { value: "氯喹", label: "氯喹" },
                { value: "羟氯喹", label: "羟氯喹" },
                { value: "蒿甲醚", label: "蒿甲醚" },
                { value: "青蒿琥酯", label: "青蒿琥酯" },
                { value: "双氢青蒿素", label: "双氢青蒿素" }
              ]
            },
            {
              value: "抗丝虫病及抗黑热病药",
              label: "抗丝虫病及抗黑热病药",
              children: [
                { value: "乙胺嗪", label: "乙胺嗪" },
                { value: "葡萄糖酸锑钠", label: "葡萄糖酸锑钠" },
                { value: "依米丁", label: "依米丁" }
              ]
            }
          ]
        },
        {
          value: "解热镇痛及非甾体抗炎药",
          label: "解热镇痛及非甾体抗炎药",
          children: [
            {
              value: "解热镇痛类药",
              label: "解热镇痛类药",
              children: [
                { value: "阿司匹林", label: "阿司匹林" },
                { value: "阿司匹林", label: "阿司匹林" },
                { value: "布洛芬", label: "布洛芬" },
                { value: "舒林酸", label: "舒林酸" },
                { value: "吲哚美辛", label: "吲哚美辛" },
                { value: "双氯芬酸钠", label: "双氯芬酸钠" },
                { value: "贝诺酯", label: "贝诺酯" },
                { value: "双氯芬酸钾", label: "双氯芬酸钾" },
                { value: "吡罗昔康", label: "吡罗昔康" }
              ]
            },
            {
              value: "抗痛风药",
              label: "抗痛风药",
              children: [
                { value: "别嘌醇", label: "别嘌醇" },
                { value: "苯溴马隆", label: "苯溴马隆" },
                { value: "秋水仙碱", label: "秋水仙碱" },
                { value: "丙磺舒", label: "丙磺舒" }
              ]
            },
            {
              value: "镇痛药",
              label: "镇痛药",
              children: [
                { value: "芬太尼", label: "芬太尼" },
                { value: "羟考酮", label: "羟考酮" },
                { value: "吗啡", label: "吗啡" },
                { value: "哌替啶", label: "哌替啶" },
                { value: "阿扑吗啡", label: "阿扑吗啡" },
                { value: "曲马多", label: "曲马多" },
                { value: "烯丙吗啡", label: "烯丙吗啡" },
                { value: "舒芬太尼", label: "舒芬太尼" },
                { value: "双氢可待因", label: "双氢可待因" }
              ]
            }
          ]
        }
      ],
      departmentList: [],
      doc: "",
      desc: "",
      medicines: [],
      dialogVisible: false,
      dialogVisible2: false,
      dialogVisible3: false,
      pageIndex: 1,
      pageSize: 10,
      totalNumber: 100
    };
  },
  created() {
    this.getPatientList();
    // this.getDepartmentList();
    this.getDocList();
  },
  methods: {
    getDocList() {
      let position = "医生";
      this.$axios
        .post("/api/CjwAdmin/getAdminListByPosition?position=" + position)
        .then(res => {
          this.docList = res.data;
        });
    },
    getPatientList() {
      let params = {
        patientName: this.patientName,
        phoneNumber: this.phoneNumber,
        idNumber: this.idNumber,
        sex: String(this.sex),
        pageIndex: this.pageIndex,
        pageSize: this.pageSize
      };
      this.$axios.get("api/CjwPatient/GetPatientList", { params }).then(res => {
        console.log(res.data.patientList);
        this.tableData = res.data.patientList;
        this.totalNumber = res.data.total;
      });
    },
    getDepartmentList() {
      this.$axios.get("/api/CjwDepartment/GetDepartmentList").then(res => {
        this.departmentList = res.data;
      });
    },
    handleSizeChange(val) {
      this.pageSize = val;
      this.getPatientList();
    },
    handleCurrentChange(val) {
      this.pageIndex = val;
      this.getPatientList();
    },
    addPatient() {
      this.dialogVisible = true;
      this.form = {
        patientName: "",
        phoneNumber: "",
        idNumber: "",
        birth: "",
        sex: ""
      };
    },
    medicine(patient) {
      this.medicines = "";
      this.patientId = patient.patientId;
      this.dialogVisible2 = true;
    },
    app(patient) {
      (this.doc = ""), (this.desc = ""), (this.patientId = patient.patientId);
      this.dialogVisible3 = true;
      console.log(patient);
    },
    handleClose() {
      this.dialogVisible = false;
    },
    handleClose2() {
      this.dialogVisible2 = false;
    },
    handleClose3() {
      this.dialogVisible3 = false;
    },
    formConfirm() {
      let year = this.form.birth.getFullYear();
      let month = this.form.birth.getMonth() + 1;
      let day = this.form.birth.getDate();
      // console.log(year, month, day);
      // let birthP = new Date(year, month, day);
      let birthP = `${year}-${month}-${day}`;
      // console.log(birthP);
      let params = {
        patientName: this.form.patientName,
        phoneNumber: this.form.phoneNumber,
        idNumber: this.form.idNumber,
        birth: birthP,
        sex: this.form.sex
      };
      console.log(params);
      this.$axios.post("api/CjwPatient/InsertPatient", params).then(res => {
        this.dialogVisible = false;
        this.getPatientList();
      });
    },
    form2Confirm() {
      console.log([1, 2].toString());
      console.log(this.medicines.toString());
      let params = {
        recordId: 0,
        patientId: this.patientId,
        inquiryId: this.adminName,
        inquiryTime: Date(),
        inquiryDesc: this.desc,
        inquiryMedicine: this.medicines.toString(),
        docId: this.doc,
        docTime: "",
        docDesc: "",
        docMedicine: "",
        departmentId: 0,
        sickBedId: 0,
        inHospitalTime: "",
        nurseId: "",
        leaveHospitalTime: "",
        isTransfer: 0,
        leaveHospitalDesc: "",
        state: 6
      };
      console.log(params);
      this.$axios
        .post("api/CjwPatient/InsertPatientRecord", params)
        .then(res => {
          this.$message({
            message: "开药成功",
            type: "success"
          });
          this.dialogVisible2 = false;
          let op = {
            opAdminName: this.adminName,
            opTable: "前台开药",
            opData: `病人id:${params.patientId}药品:${params.inquiryMedicine}`
          };
          this.$axios.post("/api/CjwOperationList/InsertOpData", op);
        });
    },
    form3Confirm() {
      let params = {
        recordId: 0,
        patientId: this.patientId,
        inquiryId: this.adminName,
        inquiryTime: Date(),
        inquiryDesc: this.desc,
        inquiryMedicine: "",
        docId: this.doc,
        docTime: "",
        docDesc: "",
        docMedicine: "",
        departmentId: 0,
        sickBedId: 0,
        inHospitalTime: "",
        nurseId: "",
        leaveHospitalTime: "",
        isTransfer: 0,
        leaveHospitalDesc: "",
        state: 2
      };
      console.log(params);
      this.$axios
        .post("/api/CjwPatient/InsertPatientRecord", params)
        .then(res => {
          this.$message({
            message: "预约医生挂号成功",
            type: "success"
          });
          this.dialogVisible3 = false;
          let op = {
            opAdminName: this.adminName,
            opTable: "预约医生挂号",
            opData: `病人id:${params.patientId}预约医生:${params.docId}`
          };
          this.$axios.post("/api/CjwOperationList/InsertOpData", op);
        });
    },
    casChange(val) {
      console.log(val);
    }
  }
};
</script>

<style scoped>
.iptRow {
  display: flex;
  margin: 10px;
}

.iptRow .el-input {
  width: 200px;
  padding-right: 20px;
}

.btnRow {
  margin: 10px;
}

.page {
  position: absolute;
  bottom: 40px;
  left: 50%;
  transform: translateX(-50%);
}

.el-dialog {
  width: 300px;
}

.el-date-picker {
  width: 100%;
}

.iptRow .el-select {
  padding-right: 20px;
}
</style>